namespace SilverSea.Untilities
{
    using System;
    using System.Diagnostics;

    public class SystemResourceMonitor
    {
        private bool bool_0;
        protected PerformanceCounter cpuCounter;
        protected PerformanceCounter ramCounter;

        public void StartMonitoring()
        {
            try
            {
                this.cpuCounter = new PerformanceCounter();
                this.ramCounter = new PerformanceCounter();
                this.cpuCounter.CategoryName = "Processor";
                this.cpuCounter.CounterName = "% Processor Time";
                this.cpuCounter.InstanceName = "_Total";
                this.ramCounter.CategoryName = "Memory";
                this.ramCounter.CounterName = "Available MBytes";
                this.bool_0 = true;
            }
            catch (Exception)
            {
                this.bool_0 = false;
            }
        }

        public void StopMonitoring()
        {
            this.bool_0 = false;
            try
            {
                if (this.cpuCounter != null)
                {
                    this.cpuCounter.Dispose();
                }
                if (this.ramCounter != null)
                {
                    this.ramCounter.Dispose();
                }
            }
            catch (Exception)
            {
            }
        }

        public int CPU_Usage
        {
            get
            {
                if (this.cpuCounter != null)
                {
                    return (int) Math.Ceiling((double) this.cpuCounter.NextValue());
                }
                return 0;
            }
        }

        public bool IsRunning
        {
            get
            {
                return this.bool_0;
            }
        }

        public int RAM_Available
        {
            get
            {
                if (this.ramCounter != null)
                {
                    return (int) Math.Ceiling((double) this.ramCounter.NextValue());
                }
                return 0;
            }
        }
    }
}

